package servlet;

import com.alibaba.fastjson.JSON;
import exception.SecKillException;
import model.Orders;
import model.Seckill;
import model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import service.OrdersService;
import service.SeckillService;
import service.impl.OrderServiceImpl;
import service.impl.SeckillServiceImpl;
import utils.BaseServlet;
import vo.SeckillVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author one world
 * @Date 2020/9/22 0022 21:32
 */
@WebServlet("/seckill")
public class SecKillServlet extends BaseServlet {
    private static final Logger logger = LoggerFactory.getLogger(SecKillServlet.class);
    private SeckillService service = new SeckillServiceImpl();
    private OrdersService ordersService = new OrderServiceImpl();
    public void seckill(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return;
        }else {
            Integer user_id = user.getId();
            Integer seckill_id = Integer.parseInt(request.getParameter("seckill_id"));
            try {
                service.secKill(user_id, seckill_id, service.findById(seckill_id).getNum());
                response.getWriter().write("SUCCESS");
            } catch (SecKillException e) {
                logger.info("秒杀不成功",e);
                response.getWriter().write(JSON.toJSONString(e.getMessage()));
            }
        }
    }
    public void startSecKill(HttpServletRequest request, HttpServletResponse response){
        service.startSecKill();
    }

    public void getSeckillList(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String pageStr = request.getParameter("page");
        String limitStr =request.getParameter("limit");
        int page = 1;
        int limit =10;
        if(pageStr!=null&&!pageStr.equals("")){
          page=Integer.parseInt(pageStr);
        }
        if(limitStr!=null&&!limitStr.equals("")){
            limit=Integer.parseInt(limitStr);
        }
        List<Seckill> seckills = service.findAll(page,limit);
        List<SeckillVo> seckillVos = new ArrayList<>();
        for(Seckill seckill:seckills){
            SeckillVo seckillVo = new SeckillVo(seckill);
            seckillVos.add(seckillVo);
        }
        Map map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",service.findCount());
        map.put("data",seckillVos);
        response.getWriter().write(JSON.toJSONString(map));
        response.getWriter().close();
    }
    public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String seckill_id = request.getParameter("seckill_id");
        if(seckill_id==null||seckill_id.equals("")){
            return;
        }
        int id = Integer.parseInt(seckill_id);
        if(service.del(id)){
            response.getWriter().write("SUCCESS");
        }else{
            response.getWriter().write("FAILED");
        }
    }
}
